<?php

namespace App\Kuafu\WaiMai\Service;

use App\Kuafu\Mqtt\Enum\MqttCodeEnum;
use App\Kuafu\Mqtt\Service\MqttService;
use App\Kuafu\Order\Enum\CodeEnum;
use App\Kuafu\WaiMai\Enum\WaiMaiDBEnum;
use App\Kuafu\WaiMai\Model\WaiMaiOrderModel;

class WaiMaiMqttService extends MqttService
{
    public static function getIns(int $shopId): self
    {
        return new self(0, $shopId);
    }

    /**
     * 外卖推送
     * @param int $sourceType
     * @param int $orderId
     * @param int $operateType
     * @return void
     */
    public function takeOutOrderOperate(int $sourceType, int $orderId, int $operateType, array $param = [])
    {
        $order = WaiMaiOrderModel::suffix($this->shopId)
                                 ->where('orderId', $orderId)
                                 ->first();
        if (!$order) {
            return;
        }

        $data = null;

        switch ($operateType) {
            case MqttCodeEnum::TAKE_OUT_OPERATE_TYPE_2:

                $expectFast = $order->expectFast;
                if ($expectFast) {
                    $expectTime = 0;
                } else {
                    $expectTime = $order->expectTime;
                }
                $data = ['fast' => $expectFast, 'expectTime' => $expectTime];
                break;
            case MqttCodeEnum::TAKE_OUT_OPERATE_TYPE_8:
            case MqttCodeEnum::TAKE_OUT_OPERATE_TYPE_10:

                $expectFast = $order->expectFast;
                if ($expectFast) {
                    $expectTime = 0;
                } else {
                    $expectTime = $order->expectTime;
                }

                $data = [
                    'serialNum' => (CodeEnum::SOURCE_TYPES[$order->sourceType] ?? '') . '#' . $order->serialNum,
                    'fast'       => $expectFast,
                    'expectTime' => $expectTime,
                    'silentSwitch' => $param['silentSwitch'] ?? WaiMaiDBEnum::SILENT_DEFAULT_SWITCH,
                ];
                break;
            case MqttCodeEnum::TAKE_OUT_OPERATE_TYPE_9:
                $data = [
                    'serialNum' => (CodeEnum::SOURCE_TYPES[$order->sourceType] ?? '') . '#' . $order->serialNum,
                    'expectTime' => $order->expectTime,
                ];
                break;
        }

        // MQTT
        $this->takeOutOrder($orderId, $sourceType, $operateType, $data);
    }
}
